package com.teaphy.coroutinecrawler.flow

import kotlinx.coroutines.*
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.channelFlow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.flow
import kotlin.time.ExperimentalTime
import kotlin.time.measureTime

@ExperimentalCoroutinesApi
@ExperimentalTime
fun main() = runBlocking {

    val timeFlow = measureTime {
        flow<String> {
            val data = listOf<String>("A", "B", "C")
            data.forEach {
                println("Flow - 发送数据：$it")
                emit(it)
                delay(100)
            }
        }.collect {
            delay(100)
            println("Flow - 接收数据: $it")
        }
    }

    println("timeFlow: $timeFlow")

    val timeChanelFlow = measureTime {
        channelFlow {
            val data = listOf<String>("A", "B", "C")
            data.forEach {
                println("ChannelFlow 发送数据：$it")
                send(it)
                delay(100)
            }
        }.collect {
            delay(100)
            println("ChannelFlow 接收数据: $it")
        }
    }

    println("timeChanelFlow: $timeChanelFlow")

    println("Done")
}